#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname extra $PACKAGE); if (($? != 0)); then exit 1; fi;
PKG_VERSION=$(pkgversion extra $PACKAGE); if (($? != 0)); then exit 2; fi;
PKG_ARCHIVE=$(pkgarchive extra $PACKAGE); if (($? != 0)); then exit 3; fi;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE); if (($? != 0)); then exit 4; fi;
PKG_DEST=/usr/pkg/$PKG_NAME/$PKG_VERSION;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
gpg2 --verify ${PKG_ARCHIVE}.tar.gz.asc ${PKG_ARCHIVE}.tar.gz || {
    # if failed, try receive public keys and verify again
    gpg2 --pgp2 --keyserver pgp.mit.edu --recv-keys 0x749D7889 && \
    gpg2 --verify ${PKG_ARCHIVE}.tar.gz.asc ${PKG_ARCHIVE}.tar.gz;
} && \
cd src && \
sed -e "s@python2.5/Python.h@& python2.7/Python.h@g" \
    -e "s@-lpython2.5]@&,\n  AC_CHECK_LIB(python2.7,main,[PYTHON_LIB=-lpython2.7])@g" \
    -i configure.in && \
sed -e 's@\^u}@^u cols 300}@' \
    -i tests/dejagnu/config/default.exp


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
autoconf && \
./configure --prefix=/usr            \
            --sysconfdir=/etc        \
            --localstatedir=/var/lib \
            --with-system-et         \
            --with-system-ss         \
            --with-system-verto=no   \
            --enable-dns-for-realm \
            --with-ldap \
    && \
    make && \
    rm -vRf $PKG_DEST/inst && \
    mkdir -pv $PKG_DEST/inst/{usr/{bin,lib,include,share},lib,bin} && \
    make DESTDIR=$PKG_DEST/inst install




#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
for LIBRARY in gssapi_krb5 gssrpc k5crypto kadm5clnt kadm5srv \
               kdb5 kdb_ldap krad krb5 krb5support verto
do
    chmod -v 755 $PKG_DEST/inst/usr/lib/lib$LIBRARY.so || exit 10;
done && \
mv -v $PKG_DEST/inst/usr/lib/libkrb5.so.3*        $PKG_DEST/inst/lib && \
mv -v $PKG_DEST/inst/usr/lib/libk5crypto.so.3*    $PKG_DEST/inst/lib && \
mv -v $PKG_DEST/inst/usr/lib/libkrb5support.so.0* $PKG_DEST/inst/lib && \
ln -v -sf ../../lib/libkrb5.so.3.3        $PKG_DEST/inst/usr/lib/libkrb5.so        && \
ln -v -sf ../../lib/libk5crypto.so.3.1    $PKG_DEST/inst/usr/lib/libk5crypto.so    && \
ln -v -sf ../../lib/libkrb5support.so.0.1 $PKG_DEST/inst/usr/lib/libkrb5support.so && \
mv -v $PKG_DEST/inst/usr/bin/ksu $PKG_DEST/inst/bin && \
chmod -v 755 $PKG_DEST/inst/bin/ksu   && \
install -v -dm755 $PKG_DEST/inst/usr/share/doc/krb5-${PKG_VERSION} && \
cp -vfr ../doc/*  $PKG_DEST/inst/usr/share/doc/krb5-${PKG_VERSION} && \
unset LIBRARY



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
echo "# Package information file... generated at make process" > $PKG_DEST/pkg.info && \
echo "# pkg name   source archive    version" >> $PKG_DEST/pkg.info && \
echo "$PKG_NAME  $PKG_ARCHIVE  $PKG_VERSION" >> $PKG_DEST/pkg.info && \
if [ -d $LFS/sources/pkg/extra/$PKG_NAME ]; then cp -aRv $LFS/sources/pkg/extra/$PKG_NAME/* $PKG_DEST; fi && \
cp -avf $LFS/sources/pkg/pkgutils.sh $PKG_DEST/pkgutils.sh && \
tar -czvvf /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz -C /usr/pkg/$PKG_NAME $PKG_VERSION && \
rm -Rfv /usr/pkg/$PKG_NAME && \
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
echo "Build complete extra/$PACKAGE"
echo "Package files are stored to: /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz"
echo " --OK--"
